package tr.com.netgis.log;

import java.util.Date;
import java.util.List;

import tr.com.netgis.LocationViewActivity;
import tr.com.netgis.R;
import tr.com.netgis.pojo.NGLocation;
import tr.com.netgis.util.Utility;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @author NetGis
 * 
 */
public class LogArrayAdapter extends BaseAdapter implements OnClickListener {

	private Context context;

	private List<NGLocation> locations;

	public LogArrayAdapter(Context context, List<NGLocation> locations) {
		this.context = context;
		this.locations = locations;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public List<NGLocation> getLocations() {
		return locations;
	}

	public void setLocations(List<NGLocation> locations) {
		this.locations = locations;
	}

	public int getCount() {
		return locations.size();
	}

	public Object getItem(int position) {
		return locations.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup viewGroup) {
		final NGLocation location = locations.get(position);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.main_row, null);
		}

		ImageView detailButton = (ImageView) convertView
				.findViewById(R.id.mainImgDetail);
		detailButton.setFocusableInTouchMode(false);
		detailButton.setFocusable(false);
		detailButton.setClickable(true);
		detailButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Toast.makeText(context, "xxx", 2).show();
				showLocationDetail(location);
			}
		});

		Date tarihSaat = new Date(location.getTarihSaat());
		// TextView txtTime = (TextView)
		// convertView.findViewById(R.id.mainTxtSaat);
		// txtTime.setText(sdfTime.format(tarihSaat));

		TextView txtHiz = (TextView) convertView.findViewById(R.id.mainTxtHiz);

		TextView txtTarih = (TextView) convertView.findViewById(R.id.mainTxtTarih);
		txtTarih.setText(Utility.getFriendlyTimeShort(tarihSaat));
		if (location.isNetwork()) {
			txtTarih.setBackgroundColor(Color.BLUE);
		} else {
			txtTarih.setBackgroundColor(Color.TRANSPARENT);
		}
		
		ImageView imgStatus = (ImageView) convertView.findViewById(R.id.mainImgStatus);
		imgStatus.setImageResource(Utility.getImageResource(location));
		txtHiz.setText(location.getSpeedView());

		return convertView;
	}

	@Override
	public void onClick(View view) {
		NGLocation entry = (NGLocation) view.getTag();
		locations.remove(entry);
		// listPhonebook.remove(view.getId());
		notifyDataSetChanged();

	}

	private void showLocationDetail(NGLocation location) {
		Intent detail = new Intent(context, LocationViewActivity.class);
		detail.putExtra("location", location);
		context.startActivity(detail);
	}

}
